package com.taobao.sinan.dal.sql.executor;

public class DefaultSqlExecutorFactory implements SqlExecutorFactory {

	@Override
	public SqlExecutor parseSql(String sql, ExecutorContext session) {
		String s = lowerSql(sql);

		if (s != null) {
			if (s.startsWith("select ")) {
				return new TurnSqlExecutor(sql, session);
			} else if (s.startsWith("insert ")) {
				return new InsertFixTableExecutor(sql, session);
			} else if (s.startsWith("update ")) {
				return new UpdateFixTableExecutor(sql, session);
			} else if (s.startsWith("delete ")) {
				return new DeleteFixTableExecutor(sql, session);
			}
		}

		return null;
	}

	private String lowerSql(String sql) {
		sql = sql == null ? "" : sql.trim();
		if ("".equals(sql)) {
			return null;
		}

		return sql.toLowerCase();
	}
}
